package cn.chenfeifx.media.controls.common;

import javafx.util.Duration;

public class TimeUtils {

    public static String getTimeStr(Duration current, Duration total) {
        String totalStr = getDurationStr(total);
        String currentStr = getDurationStr(current);
        if (totalStr.length() == 8 && currentStr.length() != 8) {
            return "00:" + currentStr + " / " + totalStr;
        }
        return currentStr + " / " + totalStr;
    }


    /**
     * 将 Duration 转换为 01:24:15 或者 30:45
     * <p>
     * 当 Duration 小于一个小时会取消小时
     *
     * @param duration
     * @return
     */
    public static String getDurationStr(Duration duration) {
        int hours = (int) duration.toHours();
        Duration subtract = duration.subtract(Duration.hours(hours));
        int minutes = (int) subtract.toMinutes();
        subtract = subtract.subtract(Duration.minutes(minutes));
        int seconds = (int) subtract.toSeconds();
        String s = String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
        if (hours < 1) {
            return s;
        }
        return String.format("%02d", hours) + ":" + s;
    }

}
